package com.kangyi.user.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.kangyi.constant.MessageConstant;
import com.kangyi.dto.ApplicationDTO;
import com.kangyi.dto.UpdateGuestsStayingDTO;
import com.kangyi.entity.*;
import com.kangyi.exception.DataNotExistException;
import com.kangyi.exception.NursingHomeException;
import com.kangyi.user.mapper.*;
import com.kangyi.user.service.GuestsStayingService;
import com.kangyi.vo.FourHighVO;
import com.kangyi.vo.GuestsStayingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
@Slf4j
public class GuestsStayingServiceImpl implements GuestsStayingService {

    @Autowired
    private GuestsStayingMapper guestsStayingMapper;


    @Autowired
    private FourHighMapper fourHighMapper;

    @Autowired
    private NursingHomeMapper nursingHomeMapper;

    @Autowired
    private FeeMapper feeMapper;

    @Override
    public List<GuestsStayingVO> queryGuestsStaying(Integer id) {
        List<GuestsStayingVO> guestsStayingVO = guestsStayingMapper.queryGuestsStaying(id);
        if (CollUtil.isEmpty(guestsStayingVO)) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        for (GuestsStayingVO stayingVO : guestsStayingVO) {
            FourHighVO fourHighVO = fourHighMapper.getById(stayingVO.getId());
            stayingVO.setFourHighVO(fourHighVO);
        }

        return guestsStayingVO;
    }


    @Override
    public GuestsStayingVO inquiryGuestsStaying(Integer guestId) {
        GuestsStayingVO guestsStayingVO = guestsStayingMapper.inquiryGuestsStaying(guestId);
        if (guestsStayingVO == null) {
            throw new DataNotExistException(MessageConstant.DATA_NOT_EXIST);
        }
        FourHighVO fourHighVO = fourHighMapper.getById(guestId);
        guestsStayingVO.setFourHighVO(fourHighVO);
        return guestsStayingVO;
    }

    @Override
    public void applyForNursingHome(ApplicationDTO applicationDTO) {
        // 检查养老院是否存在
        NursingHome nursingHome = nursingHomeMapper.selectById(applicationDTO.getNursingHomeId());
        if (nursingHome == null) {
            log.error("申请的养老院不存在，养老院 ID: {}", applicationDTO.getNursingHomeId());
            throw new DataNotExistException(MessageConstant.NURSING_HOME_NOT_EXIST);
        }

        // 检查费用信息是否存在
        if (!isFeeExists(applicationDTO)) {
            log.error("申请的养老院费用信息不存在，申请信息: {}", applicationDTO);
            throw new DataNotExistException(MessageConstant.NURSING_HOME_FEE_NOT_EXIST);
        }

        // 创建入住人员信息
        GuestsStaying guestsStaying = new GuestsStaying();
        BeanUtils.copyProperties(applicationDTO, guestsStaying);
        guestsStaying.setStatus(GuestsStaying.UNDER_REVIEW);

        // 插入入住人员信息
        guestsStayingMapper.addGuestsStaying(guestsStaying);
    }

    /**
     * 检查费用信息是否存在
     * @param applicationDTO 申请信息
     * @return 是否存在费用信息
     */
    private boolean isFeeExists(ApplicationDTO applicationDTO) {
        boolean foodFeeExists = false;
        boolean roomFeeExists = false;
        boolean specialFeeExists = false;

        if (applicationDTO.getFoodFeeId() != null) {
            Fee foodFee = feeMapper.selectById(applicationDTO.getFoodFeeId());
            foodFeeExists = foodFee != null;
        }

        if (applicationDTO.getRoomFeeId() != null) {
            Fee roomFee = feeMapper.selectById(applicationDTO.getRoomFeeId());
            roomFeeExists = roomFee != null;
        }

        if (applicationDTO.getSpecialFeeId() != null) {
            Fee specialFee = feeMapper.selectById(applicationDTO.getSpecialFeeId());
            specialFeeExists = specialFee != null;
        }

        return foodFeeExists || roomFeeExists || specialFeeExists;
    }


}
